from openai import OpenAI

def llm(question):
    llm_client = OpenAI(api_key='EMPTY', base_url='http://10.0.8.201:9527/v1')


    response = llm_client.chat.completions.create(
        model='deepseek-r1',
        messages=[
            {'role': 'system', 'content': """
                    角色：你是机场的数据专家。根据mysql的create table内容。生成5条模拟数据。
                    要求：
                       - 返回格式为sql。比如insert into tablename (col1,col2) values(value1,value2),(value3,value4);
                       - 日期时间，注意小时不能超过23,分钟不能超过59,秒不能超过59
                       - values里的数据个数，必须和表字段个数一样
                    """},
            {'role':'user', 'content': question}
        ],
        temperature=0.7,
        top_p=0.8,
        max_tokens=8000,
        stream=True
    )
    # 处理流式响应
    all_content=""
    for chunk in response:
        # 提取内容
        content = chunk.choices[0].delta.content
        all_content+=content
        print(content, end="", flush=True)
    # 非流式响应
    # content=response.choices[0].message.content
    tmp_sql=get_insert_sql(all_content)
    return tmp_sql

def get_insert_sql(content):

    if content.find("```sql"):
        tmp_sql=content.split("```sql")[1].split("```")[0]
        return tmp_sql
    else:
        return None

if __name__=='__main__':
    sqls='''CREATE TABLE IF NOT EXISTS dwr_sq_chg_complaint (
`complaintid` varchar(64) COMMENT '投诉ID',
`complaintno` varchar(64) COMMENT '投诉编号',
`complainant` varchar(64) COMMENT '投诉人',
`complainanttel` varchar(64) COMMENT '投诉人电话',
`complaintcontent` varchar(64) COMMENT '投诉内容',
`complainttim` varchar(64) COMMENT '投诉时间',
`complainttype` varchar(64) COMMENT '投诉类型',
`complaintchannel` varchar(64) COMMENT '投诉渠道',
`allocatetime` varchar(64) COMMENT '分派时间',
`processingstatus` varchar(64) COMMENT '处理状态',
`processingtime` varchar(64) COMMENT '处理时间',
`whetherevaluated` varchar(64) COMMENT '是否已评价',
`evaluationresult` varchar(64) COMMENT '评价结果',
`timeoutstate` varchar(64) COMMENT '超时状态',
`timeouttime` varchar(64) COMMENT '超时时间',
`etldt` varchar(64) COMMENT '跑批日期',
`warehoustime` varchar(64) COMMENT '入库时间'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
COMMENT '旅客投诉信息';
    '''
    sql_insert=llm(sqls)